﻿using CodeBackup.Code;
using libedi.API;
using System.Data;

namespace CodeBackup
{
    public partial class FrmDatabase : Form
    {

        private EventHandler @event;

        private List<Database> Listdata;
        public FrmDatabase(EventHandler @event,List<Database> databases)
        {
            InitializeComponent();
            this.@event = @event;  
            this.Listdata = databases;
        }

        WebApi webApi = new WebApi(libedi.SourceType.CODE);
        private void FrmDatabase_Load(object sender, EventArgs e)
        {
            DataTable strings = webApi.GetDataObject<DataTable>("SHOW TABLES");
          
            foreach (DataRow row in strings.Rows)
            {
               
               int index=  this.checkedListBox1.Items.Add(row[0].ToString());

                
                if (Listdata?.Where(l => l.baseName == row[0].ToString()).FirstOrDefault() != null)
                {
                    checkedListBox1.SetItemChecked(index, true);
                }

            }

 

           
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemChecked(i, this.checkBox1.Checked);
            }

        }

      
        private void button2_Click(object sender, EventArgs e)
        {
            List<Database> list = new List<Database>();
            foreach(var item in checkedListBox1.CheckedItems)
            {
                var model= Listdata?.Where(l => l.baseName == item.ToString()).FirstOrDefault();
                list.Add(new Database { baseName = item.ToString(), cName = model==null?"":model.cName }); 
            }
            @event?.Invoke(list, null);
            this.Close();
        }
    }
}
